<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=utf-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<jsp:include page="../head.jsp"></jsp:include>
<head>
<link rel="StyleSheet" href="css/ctreeselector.css" type="text/css" />
<script type="text/javascript" src="scripts/ctreetableT.js"></script>
<script type="text/javascript" src="scripts/stringBuffer.js"></script>
<script type="text/javascript" src="scripts/ctreeselector.js"></script>
<script type="text/javascript">

function onLoa(){
	$.ajax({
		type:"POST",
		url:"TbProductServlet",
		data:"method=getProductTypeByCompanyId&num="+parseInt(Math.random()*(10000000))+"&companyId=${sessionScope.tbProductPrice==null?0:sessionScope.tbProductPrice.companyId}",
		success:function(datas){
		dr = new cTreeTable('dr',1);
		dr.config.useSelection = true;
		dr.config.rowSelection = false;
		dr.config.closeSameLevel = true;
		dr.config.useChecks = true;
		dr.config.folderChecks = true;
		dr.config.checkSubs = true;
		dr.config.showBadNodes = true;
		dr.config.iconPath = '../img/';
		dr.add(0,-1,'产品类型');
		 pArray=datas.split(",");
		  if((pArray!=""))
		  for(var i=0;i<pArray.length;i++){
		      dr.clearCookie();
			  dr.add(pArray[i].split(":")[0],pArray[i].split(":")[1],pArray[i].split(":")[2]);
		  }
		}
		});
}
		
	function selectProductType(va){
		document.getElementById('productTypeId').value='';
		document.getElementById('pro').value='';
		$.ajax({
			type:"POST",
			url:"TbProductServlet",
			data:"method=getProductTypeByCompanyId&num="+parseInt(Math.random()*(10000000))+"&companyId="+va,
			success:function(datas){
			dr = new cTreeTable('dr',1);
			dr.config.useSelection = true;
			dr.config.rowSelection = false;
			dr.config.closeSameLevel = true;
			dr.config.useChecks = true;
			dr.config.folderChecks = true;
			dr.config.checkSubs = true;
			dr.config.showBadNodes = true;
			dr.config.iconPath = '../img/';
			dr.add(0,-1,'产品类型');
			 pArray=datas.split(",");
			  if((pArray!=""))
			  for(var i=0;i<pArray.length;i++){
				  dr.clearCookie();
				  dr.add(pArray[i].split(":")[0],pArray[i].split(":")[1],pArray[i].split(":")[2]);
			  }
			}
			});
	}
 
		function showProductMinType(obj) {
			dr.config.useChecks = false;
			var selector = getTreeSelector(dr, 'DIV1', obj.id);
			selector.cssfile = "css/ctreeselector.css";
			selector.show();
		}
		
		function showChecker(inputName) {
			dr.config.useChecks = true;
			var selector = getTreeSelector(dr, 'DIV1', inputName);
			selector.cssfile = "css/ctreeselector.css";
			selector.show();
		}
		//get products by productTypeId
		function getProductByProductId(va){
			$("#productId").empty();
			$('#productDiv').empty();
			 $("<option value='-1'>选择产品</option>").appendTo("#productId");
			$.ajax({
				type:"POST",
				url:"TbProductServlet",
				data:"method=getProductByProductId&num="+parseInt(Math.random()*(10000000))+"&productTypeId="+va,
				success:function(datas){
				  if((datas!="")){
				 pArray=datas.split(",");
					 
				  for(var i=0;i<pArray.length;i++){
					  $("<option value="+pArray[i].split(":")[0]+">"+pArray[i].split(":")[1]+"</option>").appendTo("#productId");
				  }
				  }
			}
				});
		}
	//get the info of Product by productId
		function getInfoByProductId(productId){
			$('#productDiv').empty();
			$.ajax({
				type:"POST",
				url:"TbProductServlet",
				data:"method=getProductInfoByProductId&num="+parseInt(Math.random()*(10000000))+"&productId="+productId,
				success:function(datas){
				 pArray=datas.split(",");
				  if((pArray!="")){
				  for(var i=0;i<pArray.length;i++){
					  $("<b>界面</b>:<input type='text' id='productPrice'"+i+" name='productPrice'"+i+" readonly value='"+pArray[i].split(":")[0]+"' size='10' />").appendTo('#productDiv');
					  $('<b>原价</b>:<input type="text"  id="productPrice"'+i+' name="productPrice"'+i+' readonly value="'+pArray[i].split(":")[1]+'" size="10"/>').appendTo('#productDiv');
					  $('<b>数量</b>:<input type="text" id="prodcutNum"'+i+' name="productNum"'+i+' onclick="getMoney('+i+')" value="" size="10" onblur="getMoney()"/>').appendTo('#productDiv');
					  $('<b>现价</b>:<input type="text" id="productPriceNow"'+i+' name="productPriceNow"'+i+' onclick="getMoney('+i+')" value="" size="10"/>').appendTo('#productDiv');
					  $('<b>单位</b>:<input type="text" id="productUnitId"'+i+' name="productUnitId"'+i+' readonly value="'+pArray[i].split(":")[2]+'" size="10"/><br/>').appendTo('#productDiv');
				  }
				  }
			}
				});
		}
//get the totalPrice by num and Unit price
		function getMoney(va){
			var productNum = $('#productNum'+va).val();
			var productPriceNow = $('#productPriceNow'+va).val();
			var productPrice = $('#productPrice'+va).val();
		}
		function getCompanyBycontractCode(va){
			$('#companyName').val(document.getElementById('companyName'+va).value);
			$('#companyId').val(document.getElementById('companyId'+va).value);
			$('#productMaxType').empty();
			$.ajax({
				type:"POST",
				url:"TbCompanyProductTypeMiddleServlet",
				data:"method=getCompanyProductMaxTypeByCompanyId&num="+parseInt(Math.random()*(100000))+"&companyId="+document.getElementById('companyId'+va).value,
				success:function(datas){
					if(datas!=""){
						var mpArray=datas.split("#");
						for(var i=0;i<mpArray.length;i++){
							 $("<option value="+mpArray[i].split(":")[0]+">"+mpArray[i].split(":")[1]+"</option>").appendTo("#productMaxType");
						}
						checkProductType();
					}
				}
				}); 
		}
		
		function checkProductType(){
			if(!($('#productMaxType').length==0?false:true))
			{
				alert("请选择产品大类后再添加产品!");
				return false;
			}
		}
		
	</script>
</head>
<body onload="onLoa()">
<form id='fmTbDispatchList'  action="TbDispatchListServlet" method='post'>
<input type="hidden" name="method" id="method" value="${sessionScope.tbDispatchList.dispatchListId==null?'add':'edit'}" />
<table>
<tr>
    <td colspan="2">${sessionScope.tbDispatchList.dispatchListId==null?'新增派单':'修改派单'}
     <input type='hidden' name='dispatchListId' id='dispatchListId' value='${sessionScope.tbDispatchList.dispatchListId }' />
    </td>
</tr>
<tr>
    <td>合同号</td>
    <td>
    <select name='contractCode' id='contractCode' onchange='if(contractCode.value!=-1)getCompanyBycontractCode(contractCode.value)'>
    	<option value="-1">选择合同</option>
        <c:forEach items="${sessionScope.tbContractList==null?'':sessionScope.tbContractList}" var="tbContract" varStatus="status">
	    <option value='${tbContract.contractCode }' >${tbContract.contractCode }</option> 
       </c:forEach>
    </select>
       <c:forEach items="${sessionScope.tbContractList==null?'':sessionScope.tbContractList}" var="tbContract" varStatus="status">
	    <input type='hidden' name='companyName${tbContract.contractCode }' value='${tbContract.tbCompany.companyName }' />
	    <input type='hidden' name='companyId${tbContract.contractCode }' value='${tbContract.tbCompany.companyId }' />
       </c:forEach>
    </td>
</tr>
<tr>
    <td>派工号</td>
    <td><input type='text' name='dispatchListCode' id='dispatchListCode' value='${sessionScope.tbDispatchList.dispatchListCode }' />
    </td>
</tr>

 <tr>
    <td>派单名称</td>
    <td><input type='text' name='dispatchListName' id='dispatchListName' value='${sessionScope.tbDispatchList.dispatchListName }' /></td>
</tr>
<tr>
    <td>派单预算</td>
    <td><input type='text' name='dispatchListBudget' id='dispatchListBudget' value='${sessionScope.tbDispatchList.dispatchListBudget }' />元</td>
</tr>
<tr>
	<td>公司名称</td>
	<td>
	<input type="text" name="companyName" id="companyName" disabled value=""/>
	<input type="hidden" name="companyId" id="companyId" value="0" />
	</td>
</tr>
<tr>
    <td>产品大类</td>
    <td>
    <select name='productMaxType' id='productMaxType'>
    </select>
    <button type='button' onclick='' >添加产品</button>
    <!-- 
    <input type='text' name='pro' id="pro" class="selectorinput" value=""  readonly onclick="showSelector('productTypeId');" size='25' />
    <input type="hidden" name='productTypeId' id='productTypeId' value="0"/> 
     -->
    </td>
</tr>
<tr>
	<td>产品</td>
	<td>
	<table>
	<tr>
	<td>产品类型</td>
	<td>产品名称</td>
	<td>产品单位</td>
	<td>产品界面</td>
	<td>产品数量</td>
	<td>产品单价</td>
	<td>总价格</td>
	</tr>
	
	<tr>
	<td>
	<input type='text' name='productType' id="productType0" class="selectorinput" value=""  readonly onclick="showProductMinType(this);" size='25' />
	</td>
	<td>
	<select name='product' id='product0'>
	</select>
    </td>
	<td>
	<select name='productUnit' id='productUnit0'>
	</select>
	</td>
	<td>
	<select name='productInterface' id='productInterface0'>
	</select>
	</td>
	<td><input name='productCount' id='productCount0' size=3 /> </td>
	<td><input name='productPrice' id='productPrice0' size=3 /></td>
	<td><input name='productPriceSum' id='productPriceSum0' size=8 /></td>
	</tr>
	
	</table>
	</td>
</tr>
<tr>
    <td colspan='2' align="center"><input type="submit" name="btsubmit" id="btsubmit" value="${sessionScope.tbDispatchList.dispatchListId==null?'添加':'修改'}" /><input type='reset' value='重置' /><input type='button' value='返回' onclick='history.go(-1);' />
    <input type="hidden" name="totalMoney" id="totalMoney" value=""/>
    </td>
</tr></table>
<div id="DIV1" ></div>
</form><jsp:include page="../foot.jsp"></jsp:include>
</body>
</html>
